package com.team.book.common.util;

import com.team.book.domain.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author iw
 * @create 2017-05-09 15:36
 **/
public class UserUtil {
    public static User getCurUser(){
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        User springSecurityUser = null;
        if (authentication != null) {
            if (authentication.getPrincipal() instanceof UserDetails) {
                springSecurityUser = (User) authentication.getPrincipal();
            }
        }
        return springSecurityUser;
    }
}
